//
//  ShoppingOrdersViewController.m
//  EasyToo
//
//  Created by zhengzhe on 13-9-22.
//
//

#import "ShoppingOrdersViewController.h"
#import "OrderInfo.h"
#import "UIImageView+OnlineImage.h"
#import <QuartzCore/QuartzCore.h>
#import "ShoppingManager.h"
#import "NetworkAlertView.h"

@interface ShoppingOrdersViewController ()

@end

#define BTNTAG 200010


@implementation ShoppingOrdersViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.navigationItem setTitle:@"购买信息"];
    [self.view setBackgroundColor:BackgroundColor];

    [self initScrollview];
    
    [self initOrderView];
    [self initPayView];
    
    [[ShoppingManager shareManager] setDelegate:self];


	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    
    [[ShoppingManager shareManager] setDelegate:nil];
    
    self.ordersArray = nil;
    [_bgScrollView release];
    _bgScrollView = nil;
    [_numTf release];
    _numTf = nil;
    
    [super dealloc];
}

//实始化视图
- (void)initScrollview
{
    int width = [DeviceUtil getScreenWidth];
    int heigth = self.view.frame.size.height;
    
    _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, heigth)];
    _bgScrollView.directionalLockEnabled = YES; //只能一个方向滑动
    _bgScrollView.pagingEnabled = NO; //是否翻页
    _bgScrollView.backgroundColor = [UIColor clearColor];
    _bgScrollView.showsVerticalScrollIndicator =YES; //垂直方向的滚动指示
    _bgScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;//滚动指示的风格
    _bgScrollView.showsHorizontalScrollIndicator = NO;//水平方向的滚动指示
    CGSize newSize = CGSizeMake(width, 1000 + [self.ordersArray count]*100);
    [_bgScrollView setContentSize:newSize];
    
    [self.view addSubview:_bgScrollView];
    
    UIControl *control = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, _bgScrollView.frame.size.width, _bgScrollView.frame.size.height)];
    [control addTarget:self action:@selector(onBackgroudClick) forControlEvents:UIControlEventTouchUpInside];
    [_bgScrollView addSubview:control];
}

//初始化视图
- (void)initOrderView
{
    //初始化票务
    for (int i  = 0; i < [self.ordersArray count]; i ++) {
        OrderInfo *order = [self.ordersArray objectAtIndex:i];
        
        UIImageView *bgView = [[[UIImageView alloc] init] autorelease];
        bgView.frame = CGRectMake(10, 20+i*90, 300, 90);
        [bgView setImage:[UIImage imageNamed:@"cp_sslist_item_bg"]];
        [_bgScrollView addSubview:bgView];
        
        UIImageView *orderImage = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMinX(bgView.frame)+10, CGRectGetMinY(bgView.frame)+10,70, 70)];
        [orderImage setOnlineImage:order.goodsInfo.imgUrl];
        [_bgScrollView addSubview:orderImage];
        
        UIButton *delBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        delBtn.frame = CGRectMake(0, 0, 20, 20);
        delBtn.center = CGPointMake(CGRectGetMaxX(bgView.frame) - 20, CGRectGetMidY(bgView.frame));
        delBtn.tag = BTNTAG + i;
        [delBtn addTarget:self action:@selector(delEvent:) forControlEvents:UIControlEventTouchUpInside];
        [delBtn setBackgroundImage:[UIImage imageNamed:@"5"] forState:UIControlStateNormal];
        [_bgScrollView addSubview:delBtn];
        
        UILabel *namelbl = [[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(orderImage.frame) + 10, CGRectGetMinY(bgView.frame) + 12, 180, 42)]autorelease];
        namelbl.backgroundColor = [UIColor clearColor];
        namelbl.font = [UIFont systemFontOfSize:15];
        namelbl.lineBreakMode = UILineBreakModeTailTruncation;
        namelbl.numberOfLines = 0;
        namelbl.text = order.name;

        [_bgScrollView addSubview:namelbl];
        
        UILabel *idnumlbl = [[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(namelbl.frame), CGRectGetMaxY(namelbl.frame), 180, 20)]autorelease];
        idnumlbl.backgroundColor = [UIColor clearColor];
        idnumlbl.text = [NSString stringWithFormat:@"单价:￥%.2f  数量:%d", order.price, order.num];
        idnumlbl.font = [UIFont systemFontOfSize:15];
        idnumlbl.textColor = [UIColor redColor];
        [_bgScrollView addSubview:idnumlbl];
    }
    
}


//初始化支付视图
- (void)initPayView
{
    NSInteger index = [self.ordersArray count];
//    UIButton *addTicketBtn =[UIButton buttonWithType:UIButtonTypeCustom];
//    addTicketBtn.frame = CGRectMake(20, 20+index*90, 280, 40);
//    [addTicketBtn setBackgroundImage:[UIImage imageNamed:@"xyb"] forState:UIControlStateNormal];
//    [addTicketBtn setTitle:@"继续添加乘机人" forState:UIControlStateNormal];
//    [_bgScrollView addSubview:addTicketBtn];
//    [addTicketBtn addTarget:self action:@selector(addticketEvent) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *addrIcon = [[[UIImageView alloc]initWithFrame:CGRectMake(10, 40+index*90, 10, 10)] autorelease];
    addrIcon.image = [UIImage imageNamed:@"shurubi"];
    [_bgScrollView addSubview:addrIcon];
    
    UILabel *addrLb = [[[UILabel alloc]initWithFrame:CGRectMake(25, 30+index*90, 180, 30)]autorelease];
    addrLb.text = @"手机号码";
    addrLb.backgroundColor = [UIColor clearColor];
    [_bgScrollView addSubview:addrLb];
    
    _numTf = [[UITextField alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(addrLb.frame)+10, [DeviceUtil getScreenWidth]-20, 40)];
    _numTf.font = [UIFont boldSystemFontOfSize:30];
    _numTf.borderStyle = UITextBorderStyleRoundedRect;
    _numTf.layer.borderColor = [UIColor grayColor].CGColor;
    _numTf.layer.borderWidth = 1;
    _numTf.layer.cornerRadius = 10;
    _numTf.keyboardType = UIKeyboardTypeNumberPad;
    [_bgScrollView addSubview:_numTf];

    UIImageView *payIcon = [[UIImageView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(_numTf.frame) + 20, 10, 10)];
    payIcon.image = [UIImage imageNamed:@"select_button"];
    [_bgScrollView addSubview:payIcon];
    
    
    UILabel *payLb = [[[UILabel alloc]initWithFrame:CGRectMake(25, CGRectGetMaxY(_numTf.frame)+10, 180, 30)]autorelease];
    payLb.text = @"支付方式:";
    payLb.backgroundColor = [UIColor clearColor];
    [_bgScrollView addSubview:payLb];
    
    UIImageView *payImgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, CGRectGetMaxY(payLb.frame) + 20, 100, 30)];

    [payImgView setImage:[UIImage imageNamed:@"yinlian2"]];

    [_bgScrollView addSubview:payImgView];
    
    
    UIButton *nextStepBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    nextStepBtn.frame = CGRectMake(20, CGRectGetMaxY(payImgView.frame) + 20, 280, 40);
    [nextStepBtn setBackgroundImage:[UIImage imageNamed:@"xyb"] forState:UIControlStateNormal];
    [nextStepBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [_bgScrollView addSubview:nextStepBtn];
    
    [nextStepBtn addTarget:self action:@selector(payEvent) forControlEvents:UIControlEventTouchUpInside];
}


//确认支付
- (void)payEvent
{
    if (![DeviceUtil isMobileNumber:_numTf.text]) {
        UIAlertView *baseAlert = [[UIAlertView alloc]  initWithTitle:@"购买失败" message:@"请输入正确的手机号码"  delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil];
        [baseAlert show];
        return;
    }
    
    [[NetworkAlertView shareView] showNetView];
    [[ShoppingManager shareManager] submitOrders:self.ordersArray tel:_numTf.text];
}

//继续添加机票
- (void)addticketEvent
{
}

- (void)onBackgroudClick
{
    [_numTf resignFirstResponder];
}

- (void)delEvent:(id)sender
{
    
    if ([self.ordersArray count] < 2)
    {
        UIAlertView *alert = [[UIAlertView alloc]  initWithTitle:@"删除失败" message:@"至少保留一个订单"  delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        return;
    }
    
    UIButton *button = (UIButton*)sender;
    NSInteger index = button.tag % BTNTAG;
    [self.ordersArray removeObjectAtIndex:index];
    
    [_bgScrollView removeFromSuperview];
    [_bgScrollView release];
    _bgScrollView = nil;
    
    [self initScrollview];
    [self initOrderView];
    [self initPayView];
    
//    [_bgScrollView release];
//    _bgScrollView = nil;
//    [self init_scrollview];
}

- (void)onSubmitOrders:(BOOL)isSuccess Tn:(NSString *)tn
{
    [[NetworkAlertView shareView] delView];
    NSLog(@"tn:%@", tn);
    
    [UPPayPlugin startPay:tn sysProvide:nil spId:nil mode:PAY_MODE_REALEASE viewController:self delegate:self];
}

-(void)UPPayPluginResult:(NSString*)result
{
    NSString *alertString = nil;
    if ([result isEqualToString:@"success"])
    {
        alertString = @"交易成功";
    }
    else if ([result isEqualToString:@"cancel"])
    {
        alertString = @"交易取消";
    }
    else
    {
        alertString = @"交易失败";
    }

    UIAlertView *alert = [[UIAlertView alloc]  initWithTitle:@"支付结果" message:alertString  delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];

}

@end
